package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.common.bean.PageParamVo;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.mapper.SkuBoundsMapper;
import com.atguigu.gmall.sms.mapper.SkuFullReductionMapper;
import com.atguigu.gmall.sms.mapper.SkuLadderMapper;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import com.atguigu.gmall.sms.vo.ItemSaleVo;
import com.atguigu.gmall.sms.vo.SkuSaleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@Service("skuBoundsService")
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsMapper, SkuBoundsEntity> implements SkuBoundsService {

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuBoundsEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuBoundsEntity>()
        );

        return new PageResultVo(page);
    }
    @Autowired
    private SkuLadderMapper skuLadderMapper;
    @Autowired
    private SkuFullReductionMapper skuFullReductionMapper;

    @Override
    public void saveSales(SkuSaleVo skuSaleVo) {
        // 3.1. 保存sms_sku_bounds
       SkuBoundsEntity skuBoundsEntity=new SkuBoundsEntity();
        BeanUtils.copyProperties(skuSaleVo,skuBoundsEntity);
        List<Integer> work = skuSaleVo.getWork();
        if (!CollectionUtils.isEmpty(work)){
            skuBoundsEntity.setWork(work.get(3)^8+work.get(2)^4+work.get(1)^2+work.get(0));
        }
        this.save(skuBoundsEntity);
        // 3.2. 保存sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity=new SkuFullReductionEntity();
          BeanUtils.copyProperties(skuSaleVo,skuFullReductionEntity);
           skuFullReductionEntity.setAddOther(skuSaleVo.getFullAddOther());
           this.skuFullReductionMapper.insert(skuFullReductionEntity);
        // 3.3. 保存sms_sku_ladder
        SkuLadderEntity skuLadderEntity=new SkuLadderEntity();
        BeanUtils.copyProperties(skuSaleVo,skuLadderEntity);
           skuLadderEntity.setAddOther(skuSaleVo.getFullAddOther());
        this.skuLadderMapper.insert(skuLadderEntity);
    }


    @Override
    public List<ItemSaleVo> querySalesBySkuId(Long skuId) {
        List<ItemSaleVo> itemSaleVos =new ArrayList<>();
        // 1.查询积分优惠
        SkuBoundsEntity skuBoundsEntity= this.getOne(new QueryWrapper<SkuBoundsEntity>().eq("sku_id",skuId));
        if (skuBoundsEntity!=null){
            ItemSaleVo itemSaleVo =new ItemSaleVo();
            itemSaleVo.setType("积分");
            itemSaleVo.setDesc("送"+skuBoundsEntity.getBuyBounds()+"成长积分,"+skuBoundsEntity.getGrowBounds()+"购物积分");
            itemSaleVos.add(itemSaleVo);
        }
        // 2.查询满减优惠
        SkuFullReductionEntity skuFullReductionEntity=this.skuFullReductionMapper.selectOne(new QueryWrapper<SkuFullReductionEntity>().eq("sku_id",skuId));
        if (skuFullReductionEntity!=null){
            ItemSaleVo itemSaleVo =new ItemSaleVo();
            itemSaleVo.setType("满减");
            itemSaleVo.setDesc("满"+skuFullReductionEntity.getFullPrice()+"减"+skuFullReductionEntity.getReducePrice());
            itemSaleVos.add(itemSaleVo);
        }
        // 3.查询打折优惠
        SkuLadderEntity skuLadderEntity =this.skuLadderMapper.selectOne(new QueryWrapper<SkuLadderEntity>().eq("sku_id",skuId));
        if (skuLadderEntity!=null){
            ItemSaleVo itemSaleVo =new ItemSaleVo();
            itemSaleVo.setType("折扣");
            itemSaleVo.setDesc("满"+skuLadderEntity.getFullCount()+"打"+skuLadderEntity.getDiscount());
            itemSaleVos.add(itemSaleVo);
        }
        return itemSaleVos;
    }

}